package com.doraemon.controller.user;

import com.doraemon.dto.ShoppingCartDTO;
import com.doraemon.entity.ShoppingCart;
import com.doraemon.result.Result;
import com.doraemon.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * c端购物车管理Controller
 **/
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "购物车相关接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /***
     * 查看购物车
     * ---
     * @param
     * @return: com.doraemon.result.Result<java.util.List < com.doraemon.entity.ShoppingCart>>
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list() {
        log.info("c端-查看购物车");

        // 调用Service层 getListShoppingCart 方法，获取购物车集合
        List<ShoppingCart> shoppingCarts = shoppingCartService.getListShoppingCart();

        return Result.success(shoppingCarts);
    }

    /***
     * 添加购物车
     * ---
     * @param shoppingCartDTO 购物车DTO类（前端参数）
     * @return: com.doraemon.result.Result
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("c端-添加购物车：{}", shoppingCartDTO);

        // 调用Service层 addShoppingCart 方法，添加购物车
        shoppingCartService.addShoppingCart(shoppingCartDTO);

        return Result.success();
    }

    /***
     * 清空购物车
     * ---
     * @return: com.doraemon.result.Result
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result cleanShoppingCart() {
        log.info("c端-清空购物车");

        // 调用Service层 cleanShoppingCart 方法，清空购物车
        shoppingCartService.cleanShoppingCart();

        return Result.success();
    }

    /***
     * 删除购物车中的指定商品
     * ---
     * @param shoppingCartDTO 购物车DTO类（前端参数）
     * @return: com.doraemon.result.Result
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车中的指定商品")
    public Result deleteShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("c端-删除购物车中指定商品：{}", shoppingCartDTO);

        // 调用Service层 deleteShoppingCart 方法，删除购物车中的指定商品
        shoppingCartService.deleteShoppingCart(shoppingCartDTO);

        return Result.success();
    }
}
